package mhandle

import (
	"luckapi/models"
	"luckapi/mutils"
	"strconv"
)

func AllotLuckNum(game models.Game, user models.User) (res error) {
	err, joiners := models.GetJoinLuckNumByGameId(game.Id)
	if err != nil {
		return err
	}
	result := mutils.OnlyRand(100, joiners)

	joiner := models.Joiner{}
	joiner.Game = &game
	joiner.User = &user
	joiner.Luck_num = strconv.Itoa(result)
	joiner.Datetime = mutils.NowTime()


	res = models.UpdateUserGold(100, user)
	if err != nil {
		return err
	}
	res = models.UpdateGameGold(100, game)
	if err != nil {
		return err
	}

	res =models.AddJoinerByUser(joiner)
	return res
}
